-
Notifications
You must be signed in to change notification settings - Fork 361
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix: Add test to assert unique() behaviour #233
Conversation
Codecov Report
@@ Coverage Diff @@
## main #233 +/- ##
=========================================
Coverage 58.40% 58.40%
Complexity 2044 2044
=========================================
Files 315 315
Lines 5104 5104
=========================================
Hits 2981 2981
Misses 2123 2123 Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you.
Im not sure we should have the "reset" argument in the future. It makes things unpredictable. But that is not stopping us from adding these tests.
$generator->addProvider(new class(...$words) { | ||
private $words; | ||
|
||
public function __construct(string ...$words) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the array deconstruction and not just pass the word array?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bramceulemans
Because when passing an array, it would be necessary to write more code to ensure that every element of the array is a string
:
/**
* @param array<int, string> $words
*/
public function __construct(array $words)
{
$this->words = array_values(array_map(static function (string $word): string {
return $word;
}, $words));
}
or
/**
* @param array<int, string> $words
*
* @throws \InvalidArgumentException
*/
public function __construct(array $words)
{
$notString = array_filter(static function ($word): bool {
return !is_string($word);
}, $words);
if ([] !== $notString) {
throw new \InvalidArgumentException('Every word needs to be a string');
}
$this->words = array_values($words);
}
Using variadics here allows me not to write this code.
Thank you, @Nyholm! |
This PR
Generator::unique()
Related to #155.
πββοΈ As a user of
fakerphp/faker
, I would like to continue to be able to useto generate a unique word.
I don't care if it internally uses extensions or providers, but I would like to continue using this behaviour. The tests added here ensure that this is the case.